package com.zy.web.controllers;

import com.zy.service.FileDeduplicationService;
import com.zy.utils.FileTypeValidator;
import com.zy.web.bean.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: shopping153_cloud
 * @description:
 * @author: zy
 * @create: 2025-08-19 15:52
 */
@RestController   // restful风格:    请求的method类型: get, post, put, delete,patch     返回值类型:json
@Log
@Tag(name = "文件上传", description = "支持将多文件上传到OSS的API, 支持文件类型: 图片、PDF、Word、Excel，能去重.")
public class FileUploadController {
    @Autowired
    private FileDeduplicationService fileDeduplicationService;


    @RequestMapping(  value="/upload",method = RequestMethod.POST )
    @Operation(
            summary = "多文件上传",
            description = "可上传多个文件到OSS"
    )
    public ResponseEntity< ResponseResult> upload(
            @RequestPart("uploadFiles") MultipartFile[] uploadFiles
    ){
//        try {
//            Thread.sleep( 8000 );
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }

        if (uploadFiles==null || uploadFiles.length==0) {
            //                      HTTP  400
            return  ResponseEntity.status(HttpStatus.BAD_REQUEST  ).body(  ResponseResult.builder().code(0).msg("上传文件不能为空").build() );
        }
        List<String> urls=new ArrayList<>();
        for(   MultipartFile file: uploadFiles){
            if( file.isEmpty() ){
                continue;
            }
            String contentType = file.getContentType();
            String originalFilename=file.getOriginalFilename();  //文件名
            if (!FileTypeValidator.isValidType(contentType, originalFilename)) {
                //注意:这样写的话，只要有一个文件类型不被允许，就会返回错误信息，而不是继续上传其他文件。
                return  ResponseEntity.status(HttpStatus.BAD_REQUEST  ).body(
                        ResponseResult.builder().code(0).msg("文件类型不被允许: " + originalFilename + ". 只允许图片、PDF、Word、Excel格式。").build() );
            }
            try {
                // Delegate file processing and deduplication to the service
                String fileUrl = fileDeduplicationService.processAndGetFileUrl(file);
                urls.add(fileUrl);
            } catch (Exception e) {
                e.printStackTrace();
                log.severe("上传文件到OSS失败: " + originalFilename);
            }
        }
        if(   urls.size()>0){
            log.info("上传到OSS成功,多个图片的访问地址为:" + urls);
            return ResponseEntity.status(HttpStatus.OK).body(ResponseResult.builder().code(1).msg("上传接收数据成功").data(urls).build());
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ResponseResult.builder().code(0).msg("上传接收数据失败").build());

    }
}
